package JasPerl::TagExt::JspFragment;

use strict;
use warnings;

use JasPerl::Exceptions;
use Scalar::Util qw(weaken);

sub new {
    my ($class, $method, $context, $parent) = @_;
    my $self = bless [ $method, $context, $parent ], $class;
    weaken $self->[1]; # weak ref to context
    weaken $self->[2]; # weak ref to parent
    return $self;
}

sub get_jsp_context {
    return $_[0]->[1];
}

sub get_parent {
    return $_[0]->[2];
}

sub invoke {
    my ($self, $out) = @_;
    my ($method, $context) = @{$self};

    $context->push_body($out) if $out;

    eval { $self->$method() };

    my $e = $@;
    $context->pop_body() if $out;
    # TODO: check spec for exception behavior, use from if possible
    JasPerl::JspException->throw($e) if $e;
}

1;
